package com.bestcem.xm.ticket.controller;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
import com.bestcem.xm.ticket.controller.param.NotificationParam;
import com.bestcem.xm.ticket.controller.vo.NotificationVO;
import com.bestcem.xm.ticket.controller.vo.SimpleIdVO;
import com.bestcem.xm.ticket.controller.vo.wechat.WechatUserInfoVO;
import com.bestcem.xm.ticket.service.TicketNotificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 工单 通知提醒相关接口
 *
 * @author zq.ding <zq.ding@idiaoyan.com>
 * @version 通知提醒新增微信公众号
 * @date 2021/8/31 10:38
 */
@Slf4j
@RestController
@RequestMapping(value = "/ticket/notifications", produces = MediaType.APPLICATION_JSON_VALUE)
public class TicketNotificationsController {

    @Resource
    private TicketNotificationService ticketNotificationService;

    /**
     * 获取通知提醒设置
     *
     * @param surveyId 问卷项目id
     * @return com.bestcem.xm.util.XmResultJson<com.bestcem.xm.ticket.controller.vo.NotificationVO> 通知提醒设置信息
     * @author zq.ding <zq.ding@idiaoyan.com>
     * @date 2021/9/2 11:52
     * @date 2022/4/7 12:16 XiaoYunSong 通知提醒新增微信公众号需求 新增 是否开启免打扰/是否开启微信公众号通知
     */
    @GetMapping(value = {"/{surveyId}"})
    public XmResultJson<NotificationVO> getNotification(@PathVariable String surveyId) {

        if (!ObjectId.isValid(surveyId)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT, "问卷id格式错误");
        }
        ServiceResult<NotificationVO> serviceResult = ticketNotificationService.getNotificationBySurveyId(surveyId);
        if (serviceResult.isSuccess()) {
            return XmResultJson.success(serviceResult.getData());
        }

        return XmResultJson.fail(serviceResult.getErrorData(), serviceResult.getMsg());
    }

    /**
     * 更新通知提醒设置
     *
     * @param surveyId 问卷项目id
     * @param param    前端传来的通知提醒参数
     * @return com.bestcem.xm.util.XmResultJson<com.bestcem.xm.ticket.controller.vo.SimpleIdVO> 问卷项目id
     * @author zq.ding <zq.ding@idiaoyan.com>
     * @date 2021/9/2 11:51
     * @date 2022/4/7 12:16 XiaoYunSong 通知提醒新增微信公众号需求 新增 是否开启免打扰/是否开启微信公众号通知
     */
    @PutMapping(value = {"/{surveyId}"})
    public XmResultJson<SimpleIdVO> updateNotification(@PathVariable String surveyId, @RequestBody NotificationParam param) {
        if (!ObjectId.isValid(surveyId)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT, "问卷id格式错误");
        }
        if (Objects.isNull(param)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_LOST, ReqErrorCodeEnum.PARAM_LOST.getMsg());
        }

        ServiceResult<SimpleIdVO> serviceResult = ticketNotificationService.updateNotification(surveyId, param);

        if (serviceResult.isSuccess()) {
            return XmResultJson.success(serviceResult.getData());
        }
        return XmResultJson.fail(serviceResult.getErrorData(), serviceResult.getMsg());
    }


    /**
     * 获取通知提醒微信订阅用户信息
     * 通知提醒新增微信公众号需求新增
     *
     * @param surveyId 问卷项目id
     * @return 订阅了问卷的微信用户信息
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2022/4/1 16:53
     */
    @GetMapping(value = "/wechat/{surveyId}")
    public XmResultJson<List<WechatUserInfoVO>> getNotificationSubscribers(@PathVariable String surveyId) {
        if (!ObjectId.isValid(surveyId)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT, "问卷id格式错误");
        }

        ServiceResult<List<WechatUserInfoVO>> serviceResult = ticketNotificationService.getNotificationSubscribers(surveyId);
        if (serviceResult.isSuccess()) {
            return XmResultJson.success(serviceResult.getData());
        }
        return XmResultJson.fail(serviceResult.getErrorData(), serviceResult.getMsg());
    }


    /**
     * 移除通知提醒微信订阅用户信息
     * 通知提醒新增微信公众号需求新增
     *
     * @param surveyId      问卷项目id
     * @param wechatOpenIds 需要移除订阅的微信openId列表
     * @return 成功移除的微信用户的openId列表
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2022/4/7 11:30
     */
    @DeleteMapping(value = "/wechat/{surveyId}")
    public XmResultJson<Object> removeNotificationSubscribers(@PathVariable String surveyId,
                                                              @RequestBody List<String> wechatOpenIds) {
        if (!ObjectId.isValid(surveyId)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT, "问卷id格式错误");
        }
        if (CollectionUtils.isEmpty(wechatOpenIds)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_LOST, ReqErrorCodeEnum.PARAM_LOST.getMsg());
        }

        ServiceResult<Object> serviceResult =
                ticketNotificationService.removeNotificationSubscribers(surveyId, wechatOpenIds);
        if (serviceResult.isSuccess()) {
            return XmResultJson.success(serviceResult.getData());
        }
        return XmResultJson.fail(serviceResult.getErrorData(), serviceResult.getMsg());
    }
}
